2020-2021 Sunseeker Telemetry and Lighting System
usci_b_spi_ex1_master.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
75 //*****************************************************************************
76 
77 //*****************************************************************************
78 //
79 //Specify desired frequency of SPI communication
80 //
81 //*****************************************************************************
82 #define SPICLK 500000
83 
84 uint8_t transmitData = 0x00, receiveData = 0x00;
85 uint8_t returnValue = 0x00;
86 
87 void main (void)
88 {
89  //Stop watchdog timer
90  WDT_A_hold(WDT_A_BASE);
91 
92  //Set P1.1 for slave reset
94 
95  GPIO_PORT_P1,
96  GPIO_PIN1
97  );
98 
99  //Set P1.1 for slave reset
100  //Set P1.0 to output direction
101  GPIO_setAsOutputPin(
102  GPIO_PORT_P1,
103  GPIO_PIN0
104  );
105 
106  //P3.5,4,0 option select
107  GPIO_setAsPeripheralModuleFunctionInputPin(
108  GPIO_PORT_P3,
109  GPIO_PIN1 + GPIO_PIN2 + GPIO_PIN3
110  );
111 
112  //Initialize Master
113  USCI_B_SPI_initMasterParam param = {0};
114  param.selectClockSource = USCI_B_SPI_CLOCKSOURCE_SMCLK;
115  param.clockSourceFrequency = UCS_getSMCLK();
116  param.desiredSpiClock = SPICLK;
117  param.msbFirst = USCI_B_SPI_MSB_FIRST;
118  param.clockPhase = USCI_B_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT;
119  param.clockPolarity = USCI_B_SPI_CLOCKPOLARITY_INACTIVITY_HIGH;
120  returnValue = USCI_B_SPI_initMaster(USCI_B0_BASE, &param);
121 
122  if (STATUS_FAIL == returnValue){
123  return;
124  }
125 
126  //Enable SPI module
127  USCI_B_SPI_enable(USCI_B0_BASE);
128 
129  //Enable Receive interrupt
130  USCI_B_SPI_clearInterrupt(USCI_B0_BASE, USCI_B_SPI_RECEIVE_INTERRUPT);
131  USCI_B_SPI_enableInterrupt(USCI_B0_BASE, USCI_B_SPI_RECEIVE_INTERRUPT);
132 
133  //Now with SPI signals initialized, reset slave
135  GPIO_PORT_P1,
136  GPIO_PIN1
137  );
138 
139  //LED On
141  GPIO_PORT_P1,
142  GPIO_PIN0
143  );
144 
145  //Wait for slave to initialize
146  __delay_cycles(100);
147 
148  //Initialize data values
149  transmitData = 0x00;
150 
151  //USCI_A0 TX buffer ready?
152  while (!USCI_B_SPI_getInterruptStatus(USCI_B0_BASE,
153  USCI_B_SPI_TRANSMIT_INTERRUPT)) ;
154 
155  //Transmit Data to slave
156  USCI_B_SPI_transmitData(USCI_B0_BASE, transmitData);
157 
158  //CPU off, enable interrupts
159  __bis_SR_register(LPM0_bits + GIE);
160 }
161 
162 //******************************************************************************
163 //
164 //This is the USCI_B0 interrupt vector service routine.
165 //
166 //******************************************************************************
167 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
168 #pragma vector=USCI_B0_VECTOR
169 __interrupt
170 #elif defined(__GNUC__)
171 __attribute__((interrupt(USCI_B0_VECTOR)))
172 #endif
173 void USCI_B0_ISR (void)
174 {
175  switch (__even_in_range(UCB0IV,4)){
176  //Vector 2 - RXIFG
177  case 2:
178  //USCI_A0 TX buffer ready?
179  while (!USCI_B_SPI_getInterruptStatus(USCI_B0_BASE,
180  USCI_B_SPI_TRANSMIT_INTERRUPT)) ;
181 
182  receiveData = USCI_B_SPI_receiveData(USCI_B0_BASE);
183 
184  //Increment data
185  transmitData++;
186 
187  //Send next value
188  USCI_B_SPI_transmitData(USCI_B0_BASE,
190  );
191 
192  //Delay between transmissions for slave to process information
193  __delay_cycles(40);
194 
195  break;
196  default: break;
197  }
198 }
199 
MPU_initThreeSegmentsParam param
#define STATUS_FAIL
Definition: hw_memmap.h:23
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)
uint8_t receiveData
void main(void)
void USCI_B0_ISR(void)
uint8_t transmitData
uint8_t returnValue
#define SPICLK